﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPIMobileInterface.Entities;
using PPIDesktopClient.General;
using System.Collections;
using PPIDesktopClient.Logic;

namespace PPIDesktopClient.General
{
    /// <summary>
    /// Class that manages the deploy process. Requires the full path for the mobile database. 
    /// It is activated by calling StartDeploy();
    /// </summary>
    public class Deploy
    {

        #region Protected Properties

        protected string m_fileName;
        protected ESettingsStructure m_Settings;
        
        #endregion


        public Deploy(string FileName, ESettingsStructure settings)
        {
            m_fileName = FileName;
            m_Settings = settings;
        }
        public Deploy(string FileName)
        {
            m_fileName = FileName;
            m_Settings = null;
        }

        /// <summary>
        /// StartDeploy verifies the existence of the needed tables. If they dont exists it will create them. Inserts data in the mobile database
        /// </summary>
        public void StartDeploy()
        {
            CheckRequiredTables();
        }

        private void CheckRequiredTables()
        {
            try
            {
                PPIMobileInterface.Utils.IsProjectTableCreated(m_fileName);
                LVendor_Project.InsertProjects(m_fileName);

                PPIMobileInterface.Utils.IsUsersTableCreated(m_fileName);
                LVendor_Contacts.InsertContacts(m_fileName);

                PPIMobileInterface.Utils.IsUtilityTableCreated(m_fileName);
                LUtility.InsertUtilities(m_fileName);

                PPIMobileInterface.Utils.IsVendorTableCreated(m_fileName);
                LVendor.InsertVendors(m_fileName);


                if (m_Settings != null)
                    InsertValues();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private void InsertValues()
        {
            LVendor_Visit_Type.InsertVisitType(m_fileName);
            LXMissionStructures_TopType.InsertTopType(m_fileName);
            LXMissionStructures_PoleClass.InsertClass(m_fileName);
            LXMissionStructures_PoleSpecies.InsertSpecies(m_fileName);
            LXMissionStructures_PoleTreatments.InsertTreatments(m_fileName);
            PPIMobileInterface.Logic.LogicLayer.InserSettings(m_Settings, m_fileName);
        }
    }
}
